import { defineStore } from 'pinia'
// interface Ibook {
//   id: number
//   title: string
//   author: string
//   price: number
//   discount: number
//   bookConcern: string
//   imgUrl: string
//   bigImgUrl: string
//   publishDate: string
//   brief: string
//   inventory: number
//   detail: string
//   newness: boolean
//   hot: boolean
//   specialOffer: boolean
//   slogan: string
//   category: {
//     id: number
//     name: string
//     root: boolean
//     parentId: number
//     children: null
//   }
// }
interface IBook {
  id: number
  imgUrl: string
  quantity: number
  title: string
  price: number
  inventory?: number
  author?: string
  bigImgUrl?: string
  bookConcern?: null
  brief?: null
  discount?: number
  publishDate?: null
}
export const useCartStore = defineStore('cart', {
  state: () => ({
    // message: '',
    items: [
      // {
      //   author: '',
      //   bigImgUrl: '',
      //   bookConcern: null,
      //   brief: null,
      //   discount: 0,
      //   id: 0,
      //   imgUrl: '',
      //   inventory: 0,
      //   price: 0,
      //   publishDate: null,
      //   title: '',
      //   quantity: 0,
      // },
    ] as IBook[],
  }),
  persist: true,
  getters: {
    // fullMessage: (state) => `The message is "${state.message}".`,
    //计算购物车中所有商品的总价
    cartTotalPrice: (state) =>
      state.items.reduce((total, product) => {
        return total + product.price * product.quantity
      }, 0),
    //计算购物车中单项商品的价格
    cartItemPrice: (state) => (id: number) => {
      if (state.items.length > 0) {
        const cartItem = state.items.find((item) => item.id === id)
        if (cartItem) {
          return cartItem.price * cartItem.quantity
        }
      }
    },
    //获取购物车中商品的数量
    itemsCount: (state) => {
      return state.items.length
    },
  },
  actions: {
    // 异步更新 message
    // async updateMessage(newMessage: string): Promise<string> {
    //   return new Promise((resolve) => {
    //     setTimeout(() => {
    //       // 这里的 this 是当前的 Store 实例
    //       this.message = newMessage
    //       resolve('Async done.')
    //     }, 3000)
    //   })
    // },
    // 同步更新 message
    // updateMessageSync(newMessage: string): string {
    //   // 这里的 this 是当前的 Store 实例
    //   this.message = newMessage
    //   return 'Sync done.'
    // },
    //增加任意数量的商品到购物车
    addProductToCart(item: IBook) {
      const { id, imgUrl, title, price, inventory, quantity } = item
      if (inventory && inventory > 0) {
        const cartItem = this.items.find((item) => item.id == id)
        if (!cartItem) {
          this.pushProductToCart({ id, imgUrl, title, price, quantity })
          console.log('pushCart')
        } else {
          this.incrementItemQuantity({ id, quantity })
          console.log('increment')
        }
      }
    },
    // 添加商品到购物车
    pushProductToCart(item: any) {
      let { id, imgUrl, title, price, quantity } = item
      console.log(quantity)
      if (!quantity) {
        quantity = 1
      }
      this.items.push({
        id,
        imgUrl,
        title,
        price,
        quantity,
      })
      console.log(this.items)
    },
    //增加商品数量
    incrementItemQuantity({ id, quantity }: { id: number; quantity: number }) {
      let cartItem = this.items.find((item) => item.id === id)
      if (cartItem) {
        cartItem.quantity += quantity
      }
    },
    //用于清空购物车
    setCartItems({ items }: any) {
      this.items = items
    },
    // 删除购物车中的商品
    deleteCartItem(id: number) {
      let index = this.items.findIndex((item) => item.id === id)
      if (index > -1) {
        this.items.splice(index, 1)
      }
    },
  },
})
